package com.threeonemeeting.tab;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;

import com.mobclick.android.MobclickAgent;
import com.threeonemeeting.Const;
import com.threeonemeeting.activity.BaseActivity;
import com.threeonemeeting.activity.SendCommentActivity;

// 支持多个Activity跳转的Tab基类
public class MultiActivityTab extends ActivityGroup {

	protected LinearLayout mTopLayout;
	protected LinearLayout.LayoutParams mLinearLayoutParams;
	protected LocalActivityManager mActivityManager;

	// 当前Tab中所包含的所有的Activity，每个Activity一个名称
	// 与它们的类名一致
	// 当前的Activity名称
	protected String strCurrentActivityName = "";
	protected Map<String, Intent> mContents;
	protected Map<String, LinearLayout> mLayouts;
	protected Map<String, BaseTabActivity> mTabActivities;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivityManager = getLocalActivityManager();
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 外部的Layout
		mLinearLayoutParams = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		mTopLayout = new LinearLayout(this);
		mTopLayout.setBackgroundColor(0xFFFFFFFF);
		mTopLayout.setLayoutParams(mLinearLayoutParams);
		mTopLayout.setOrientation(LinearLayout.VERTICAL);

		mContents = new HashMap<String, Intent>();
		mLayouts = new HashMap<String, LinearLayout>();
		mTabActivities = new HashMap<String, BaseTabActivity>();

		setContentView(mTopLayout);
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	// 增加一个Activity，在布局中增加一个LinearLayout，同时将其设置为不可见
	public void addActivity(String strToActivityName, Intent intent) {
		if (strToActivityName == null || intent == null)
			return;

		// 检查strToActivityName的Activity是否已经存在，如果不存在，则添加
		int nId = Math.abs(strToActivityName.hashCode());
		View addedView = this.findViewById(nId);
		if (addedView == null) {
			LinearLayout activityLayout = new LinearLayout(this);
			activityLayout.setId(nId);
			activityLayout.setBackgroundColor(0xFFFFFFFF);
			activityLayout.setLayoutParams(mLinearLayoutParams);
			activityLayout.setOrientation(LinearLayout.VERTICAL);
			activityLayout.setVisibility(View.GONE);
			mTopLayout.addView(activityLayout);

			mLayouts.put(strToActivityName, activityLayout);
		}

		mContents.put(strToActivityName, intent);
	}

	private void hideAllLayout() {
		Iterator<?> it = mLayouts.entrySet().iterator();
		while (it.hasNext()) {
			@SuppressWarnings("rawtypes")
			Map.Entry entry = (Map.Entry) it.next();
			LinearLayout l = (LinearLayout) entry.getValue();
			if (l == null)
				continue;

			l.setVisibility(View.GONE);
		}

	}

	// 将某个Activity设置为可见
	public void setCurrentTab(String strToActivityName) {

		// 找到strToActivityName对应的LinearLayout
		int nId = Math.abs(strToActivityName.hashCode());
		LinearLayout layout = (LinearLayout) findViewById(nId);
		if (layout == null) {
			return;
		}

		Intent intent = mContents.get(strToActivityName);
		if (intent == null)
			return;

		hideAllLayout();

		// 将其余的LinearLayout设置为GONE
		intent.putExtra("from", strCurrentActivityName);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		View view = mActivityManager.startActivity(strToActivityName, intent)
				.getDecorView();
		layout.removeAllViews();
		layout.addView(view, mLinearLayoutParams);
		layout.setVisibility(View.VISIBLE);

		strCurrentActivityName = strToActivityName;
		mTabActivities.put(strCurrentActivityName,
				(BaseTabActivity) mActivityManager.getCurrentActivity());

		((BaseTabActivity) mActivityManager.getCurrentActivity()).refresh();
	}

	public void setCurrentTab(String strToActivityName, Bundle b) {

		// 找到strToActivityName对应的LinearLayout
		int nId = Math.abs(strToActivityName.hashCode());
		LinearLayout layout = (LinearLayout) findViewById(nId);
		if (layout == null)
			return;

		Intent intent = mContents.get(strToActivityName);
		if (intent == null)
			return;

		hideAllLayout();

		// 将其余的LinearLayout设置为GONE
		Bundle oldBundle = intent.getExtras();
		if (oldBundle != null) {
			intent.getExtras().clear();
			oldBundle = intent.getExtras();
		}

		intent.putExtra("from", strCurrentActivityName);
		intent.putExtras(b);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

		oldBundle = intent.getExtras();

		View view = mActivityManager.startActivity(strToActivityName, intent)
				.getDecorView();
		layout.removeAllViews();
		layout.addView(view, mLinearLayoutParams);
		layout.setVisibility(View.VISIBLE);

		strCurrentActivityName = strToActivityName;
		mTabActivities.put(strCurrentActivityName,
				(BaseTabActivity) mActivityManager.getCurrentActivity());
	}

	public void setCurrentTab(String strFromActivityName,
			String strToActivityName) {

		// 找到strToActivityName对应的LinearLayout
		int nId = Math.abs(strToActivityName.hashCode());
		LinearLayout layout = (LinearLayout) findViewById(nId);
		if (layout == null)
			return;

		Intent intent = mContents.get(strToActivityName);
		if (intent == null)
			return;

		hideAllLayout();

		// 将其余的LinearLayout设置为GONE
		intent.putExtra("from", strFromActivityName);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		View view = mActivityManager.startActivity(strToActivityName, intent)
				.getDecorView();
		layout.removeAllViews();
		layout.addView(view, mLinearLayoutParams);
		layout.setVisibility(View.VISIBLE);

		strCurrentActivityName = strToActivityName;
		mTabActivities.put(strCurrentActivityName,
				(BaseTabActivity) mActivityManager.getCurrentActivity());
	}

	public void setCurrentTab(String strFromActivityName,
			String strToActivityName, Bundle b) {

		// 找到strToActivityName对应的LinearLayout
		int nId = Math.abs(strToActivityName.hashCode());
		LinearLayout layout = (LinearLayout) findViewById(nId);
		if (layout == null)
			return;

		Intent intent = mContents.get(strToActivityName);
		if (intent == null)
			return;

		hideAllLayout();

		// 将其余的LinearLayout设置为GONE
		intent.putExtra("from", strFromActivityName);
		intent.putExtras(b);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		View view = mActivityManager.startActivity(strToActivityName, intent)
				.getDecorView();
		layout.removeAllViews();
		layout.addView(view, mLinearLayoutParams);
		layout.setVisibility(View.VISIBLE);

		strCurrentActivityName = strToActivityName;
		mTabActivities.put(strCurrentActivityName,
				(BaseTabActivity) mActivityManager.getCurrentActivity());
	}

	public String getCurrentTabName() {
		return strCurrentActivityName;
	}

	public BaseTabActivity getCurrentTab() {
		BaseTabActivity currentActivity = mTabActivities
				.get(strCurrentActivityName);
		return currentActivity;
	}

	public boolean canBack() {
		BaseTabActivity ac = getCurrentTab();
		if (ac == null)
			return false;

		return ac.canBack();
	}

	public void back() {
		BaseTabActivity ac = getCurrentTab();
		ac.back();
	}

	public void backToTab(String strToActivityName) {

		// 找到strToActivityName对应的LinearLayout
		int nId = Math.abs(strToActivityName.hashCode());
		LinearLayout layout = (LinearLayout) findViewById(nId);
		if (layout == null)
			return;

		hideAllLayout();

		layout.setVisibility(View.VISIBLE);
		strCurrentActivityName = strToActivityName;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Activity parent = getParent();
		if (parent != null) {
			return parent.onKeyDown(keyCode, event);
		}

		return super.onKeyDown(keyCode, event);
	}

	// ====================================================
	// startActivityForResult
	// 内嵌的tab页面没法直接处理 OnActivityResult，必须交由父类处理
	// ====================================================

	private void onSelectImage(int requestCode, int resultCode, Intent data) {
		SendCommentActivity activity = SendCommentActivity.getInstance();
		if (activity == null)
			return;

		activity.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (requestCode) {
		case Const.RequestCode.IMAGEPICK_REQUEST_CODE:
		case Const.RequestCode.CAMERA_REQUEST_CODE:
		case Const.RequestCode.SHAREFROMSD_REQUEST_CODE:
			onSelectImage(requestCode, resultCode, data);
			break;

		default:
			((BaseActivity) mActivityManager.getCurrentActivity())
					.handleOnActivityResult(requestCode, resultCode, data);

		}
	}
}
